//
//  DDInlandController.m
//  DDMusic
//
//  Created by rimi on 16/11/10.
//  Copyright © 2016年 FengBaoFamily. All rights reserved.
//

#import "DDInlandController.h"
#import "DDNetWorkTools.h"
#import "DDInlandModel.h"
#import "DDInlandCell.h"
#import "DDPublicMacro.h"
#import "DDMusicModel.h"

@interface DDInlandController ()
@property(nonatomic,strong)NSMutableArray* dataArray;
@end

@implementation DDInlandController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView.estimatedRowHeight = 106;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    NSString* url = @"http://route.showapi.com/213-4";
    __weak typeof(self)myself = self;
    NSString* showapi_appid = @"27019";
    NSString* showapi_sign = @"53931f9c4a2f4ed0ab945cb3757b4f94";
    NSString* topid = @"5";
    NSDictionary* parma = @{@"showapi_appid":showapi_appid,@"showapi_sign":showapi_sign,@"topid":topid};
    [[DDNetWorkTools shareDDNetWorkTools]requestWithMethod:GET Url:url Parameters:parma finished:^(id responseObject, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
            NSLog(@"出现异常");
            return ;
        }
//        NSLog(@"从这里开始");
//        NSLog(@"%@",responseObject);
         NSArray* dictArray = responseObject[@"showapi_res_body"][@"pagebean"][@"songlist"];
        _dataArray = [NSMutableArray arrayWithCapacity:dictArray.count];
        for (NSDictionary* dict in dictArray) {
            DDInlandModel* inland = [[DDInlandModel alloc]initWithDict:dict];
            [_dataArray addObject:inland];
        }
        [myself.tableView reloadData];
    }];
}


#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DDInlandCell *cell = [[NSBundle mainBundle]loadNibNamed:@"DDInlandCell" owner:nil options:nil].firstObject;
    DDInlandModel* inland = self.dataArray[indexPath.row];
    cell.musics = inland;
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DDInlandCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"DDMainRankTableViewCellClicked" object:@{ISPLAYNEWMUSIC:@"YES",NEWPLAYMUSICLIST:@[(DDMusicModel*)cell.musics]}];
    
}
@end
